
package Type_1_arr.title3_P977;


import java.util.Arrays;

//977. 有序数组的平方
public class P977SquareOfArray2 {


    //双指针

    public static int[] sortedSquares(int[] nums) {
        System.out.println("开始：" + Arrays.toString(nums));

        int beginIndex = 0;
        int endIndex = nums.length - 1;

        int lowIndex = nums.length - 1;

        int[] result = new int[nums.length];


        while (beginIndex <= endIndex) {
            int beginNum = nums[beginIndex] * nums[beginIndex];
            int endNum = nums[endIndex] * nums[endIndex];


            if (beginNum < endNum) {//右边大
                result[lowIndex] = endNum;
                endIndex--;
            }
            //TODO 不用拆分3种情况讨论 等于的归为一类
            else {
                result[lowIndex] = beginNum;
                beginIndex++;
            }
            lowIndex--;
        }

        return result;
    }

    public static void main(String[] args) {

//        int[] nums = {-4, -1, 0, 3, 10};
        int[] nums = {-7, -3, 2, 3, 11};


        int[] array = sortedSquares(nums);
        System.out.println("排序：" + Arrays.toString(array));
    }
}